#!/usr/local/bin/tclsh
# A script to compare two files by matching line to line.
# If both files contain the same lines, in any order, then
# they are considered the same and the script returns 0.
# Otherwise, the script returns -1.
#
# For example, the files
#
# This is line 1
# This is line 2
#
# and
#
# This is line 2
# this is line 1
#
# would be considered identical.
# However, the files
#
# This is line 2
# This is line 2 also
#
# and
#
# This is line 1
# This is line 2
#
# are different.
#


if { $argc < 2 } {
   puts "compare <file1> <file2>"
   exit -1
   }

set filename1 [lindex $argv 0]
set filename2 [lindex $argv 1]

set file1 [open $filename1]
set file2 [open $filename2]

set line ""
set infile1 ""

while { [gets $file1 line] != -1 } {
   set infile1 [linsert $infile1 end $line]
   }

set line ""
set infile2 ""

while { [gets $file2 line] != -1 } {
   set infile2 [linsert $infile2 end $line]
   }

close $file1
close $file2

set list1 ""
set list2 ""
set file1 ""
set file2 ""

if { [llength $infile1] >= [llength $infile2] } then {
   set list1 $infile1
   set file1 $filename1
   set list2 $infile2
   set file2 $filename2
   } else {
   set list2 $infile1
   set file2 $filename1
   set list1 $infile2
   set file1 $filename2
   }

foreach line $list1 {
   set index [lsearch -exact $list2 $line]
   if { $index == -1 } then {
      puts "Could not match $line from $file1 with line in $file2"
      exit -1
   } else {
      set list2 [lreplace $list2 $index $index]
      }
   }

exit 0
